﻿using AutoMapper;
using Ross.EntityFramework;
using Ross.EntityFramework.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Ross.Service
{
    public class UserInfoService : BaseService, IDisposable
    {
        private TranDbContext dbContext;
        public IRepository<Users, int> repository;

        public UserInfoService(string nameOrConnectionString)
        {
            dbContext = new TranDbContext(nameOrConnectionString);
            repository = new Repository<Users, int>(dbContext);
        }
        /// <summary>
        /// 根据内存中的用户名获取到用户的整体信息（此项不包含密码）
        /// </summary>
        /// <returns></returns>
        public ResultDto<Dto.oUser> Get()
        {
            ResultDto<Dto.oUser> result = new ResultDto<Dto.oUser>();
            try
            {
                if (oPrincipal != null)
                {
                    var obj = repository.GetAllList(o => o.UserName == username || o.EmpID == username).FirstOrDefault();
                    if (obj != null)
                    {
                        result.status = 200;
                        result.datas = Mapper.Map<Dto.oUser>(obj);
                    }
                    else
                    {
                        result.message = "user is not exist";
                        result.status = 500;
                    }
                }
                else
                {
                    result.message = "Unable to access unauthorized app";
                    result.status = 500;
                }
            }
            catch (Exception e)
            {
                result.message = e.Message;
                result.status = 500;
            }
            return result;
        }
        /// <summary>
        /// 释放资源
        /// </summary>
        public void Dispose()
        {
            dbContext.Dispose();
        }
    }
}
